<?php
class gameController extends base
{

    public function __construct($type='api')
    {
        parent::__construct($type);
    }
    
    public function doinit(){
        /*if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }*/
        $this->result['result']['isOn'] = $this->load('game')->gameIsOn()? 1 :0;
        $this->result['result']['tipsString'] = '';
        $rules = $this->db->getAll("select name,img_icon,img_big,boss_type,poi_keywords from r_game_boss_rules");
        foreach($rules as &$rule){
            $rule['poi_keywords'] = explode(',',$rule['poi_keywords']);
        }
        $this->result['result']['bossRule'] = $rules;
    }
    
    /*
     * 获得玩家信息
     */
    public function dogetUserInfo(){
        $uid = common::request('uid','G','');
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!$uid){
            $uid = $this->uid;
        }
        $userInfo = $this->load('game')->getUserInfo($uid);
        if(empty($userInfo))
        {
            $_ENV['error']->getErrors(309);
            exit;
        }
        $this->result['result'] = $userInfo;
    }
    
    /*
     * 获得宝宝信息
     */
    public function dogetMonsterInfo(){
        $id = common::request('id','G','');
        if(empty($id))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $monster = $this->load('game')->getMonsterInfo($id);
        if(!$monster){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result'] = $monster;
    }
    
    /*
     * 获得Boss信息
     */
    public function dogetBossInfo(){
        $poi = common::request('poi','G','');
        $boss_type = common::request('boss_type','G','');
        $uid = isset($this->uid) ? $this->uid : 0;
        if(!$poi){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!in_array($boss_type,[1,2])){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $game_mod = $this->load('game');
        $bossInfo = $game_mod->getBossInfo($boss_type,$poi,$uid);
        
        $this->result['result'] = $bossInfo;
    }
    
    /*
     * 获得排名
     */
    public function dogetRankList(){
        $type = common::request('type','G',0);
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $uid = $this->uid;
        $mid = $this->db->getOne("select id from r_game_monsters where uid = {$uid} limit 1");
        $game_mod = $this->load('game');
        
        $userInfo = $game_mod->getUserInfo($uid);
        $monsterInfo = $game_mod->getMonsterInfo($mid);
        $result = [];
        //击杀
        $res = $game_mod->getRankList(0,$uid);
        $result['userList'] = $res['list'];
        $userInfo['userRank'] = $res['rank'] === false ? '-' : $res['rank'] + 1;
        //城市
        $res = $game_mod->getRankList(1,$uid);
        $result['cityList'] = $res['list'];
        //属性
        $res = $game_mod->getRankList(2,$mid);
        $result['monsterList'] = $res['list'];
        $monsterInfo['monsterRank'] = $res['rank'] === false ? '-' : $res['rank'] + 1;
        //探索
        $res = $game_mod->getRankList(3,$uid);
        $result['exploreList'] = $res['list'];
        $userInfo['exploreRank'] = $res['rank'] === false ? '-' : $res['rank'] + 1;
        
        $result['userInfo'] = $userInfo;
        $result['monsterInfo'] = $monsterInfo;
        $this->result['result'] = $result;
    }
    
    /*
     * 照料列表
     */
    public function dogetCareList(){
        $page = common::request('page','G',0);
        $uid = common::request('uid','G',0);
        $monster_id = common::request('monster_id','G',0);
        
        if(!$uid){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $page && $page -= 1;
        $length = 50;
        $start = $page * $length;
        $this->result['result']['carelist'] = $this->load('game')->getCareList($uid,$start,$length);
    }
    
    /*
     * 助力列表
     */
    public function dogetBuffList(){
        $page = common::request('page','G',0);
        $uid = common::request('uid','G',0);
        $monster_id = common::request('monster_id','G',0);
        
        if(!$uid){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $page && $page -= 1;
        $length = 50;
        $start = $page * $length;
        $this->result['result']['bufflist'] = $this->load('game')->getBuffList($uid,$start,$length);
    }
    
    /*
     * 绑定账户
     */
    public function dobindUser(){
        $city = common::request('city','P','');
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $game_mod = $this->load('game');
        $user = $game_mod->getUserInfo($this->uid);
        if(!$user){
            $game_mod->bindUser($this->uid,$city);
            $user = $game_mod->getUserInfo($this->uid);
        }
        
        $this->result['result'] = $user;
    }
    
    /*
     * 拾取宝宝
     */
    public function dopickMonster(){
        $mid = $this->db->getOne("select id from r_game_monsters where uid = {$this->uid} limit 1");
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $game_mod = $this->load('game');
        if(!$mid){
            $user = $this->load('client')->getUserInfo($this->uid);
            $nickname = $user['nickname'].'的小麻雀';
            $mid = $game_mod->addMonster($this->uid,$nickname);
        }
        $this->result['result'] = $game_mod->getMonsterInfo($mid);
    }
    
    /*
     * 上传攻击结果
     */
    public function dopostAttackResult(){
        $boss_id = common::request('boss_id','P','');
        $monster_id = common::request('monster_id','P','');
        $monster_hp = common::request('monster_hp','P','');
        $monster_attack = common::request('monster_attack','P',0);
        $city = common::request('city','P','');
        $address = common::request('address','P','');
        
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!$boss_id || !$monster_id){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $game_mod = $this->load('game');
        $bossInfo = $game_mod->getBossInfo($boss_id);
        $monsterInfo = $game_mod->getMonsterInfo($monster_id);
        
        if(!$bossInfo || !$monsterInfo || $monster_hp < 0 || $monster_hp > $monsterInfo['hp']){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $team_members = $bossInfo['team_members'];
            
        //加入战斗
        if(!in_array($this->uid,$team_members)){
            $team_members[] = $this->uid;
            $row = [
                'id' => $boss_id,
                'team_members' => $team_members
            ];
            $game_mod->updateBoss($row);
            
            $row = [
                'uid' => $this->uid,
                'poi' => $bossInfo['poi'],
                'boss_type' => $bossInfo['boss_type'],
                'result' => 0,
                'city' => $city,
                'address' => $address,
                'boss_id' => $bossInfo['uniqid']
            ];
            $game_mod->addFightResult($row);
        }
        
        //更新boss信息
        $row = [
            'id'=>$boss_id,
            'monster_attack'=>$monster_attack
        ];
        $game_mod->updateBoss($row);
        $bossInfo = $game_mod->getBossInfo($boss_id);
        
        //打死
        if($bossInfo['current_hp'] == 0){
            //更新战斗信息
            $id = $this->db->getOne("select id from r_game_fight_record where uid='{$this->uid}' and boss_id = '{$bossInfo['uniqid']}' and result=0 limit 1");
            if($id){
                $row = [
                    'id'=>$id,
                    'result'=>1,
                    'uid'=>$this->uid,
                    'boss_type' => $bossInfo['boss_type']
                ];
                $game_mod->updateFightResult($row);
                
                //可领奖
                if(!in_array($this->uid,$bossInfo['gift_uids'])){
                    $gift_uids = $bossInfo['gift_uids'];
                    $gift_uids[] = $this->uid;
                    $row = [
                        'id'=> $boss_id,
                        'gift_uids' => $gift_uids,
                    ];
                    $game_mod->updateBoss($row);
                    
                    $this->result['result']['giftInfo'] = $game_mod->getGift($this->uid,$bossInfo['poi']);
                }
                
                //更新宝宝信息
                $monsterInfo = $game_mod->getMonsterInfo($monster_id);
                $monster = $game_mod->getLevelUpMonster($monsterInfo,$bossInfo['boss_type'] == 1 ? 500 : 1000);
                $monster['current_hp'] = $monster_hp;
            }
            
        }else{
        
            //更新宝宝信息
            $monster = [
                'id'=>$monster_id,
                'current_hp'=>$monster_hp
            ];
        }
        $game_mod->updateMonster($monster);
        
        $this->result['result']['bossInfo'] = $bossInfo;
        $this->result['result']['monsterInfo'] = $game_mod->getMonsterInfo($monster_id);
    }
    
    /*
     * 上传战斗结果
     */
    public function dopostFightResult(){
        $poi = common::request('poi','P','');
        $boss_type = common::request('boss_type','P',0);
        $result = common::request('result','P',0);
        $monster_id = common::request('monster_id','P',0);
        $boss_level = common::request('boss_level','P',0);
        $monster_hp = common::request('monster_hp','P',0);
        $city = common::request('city','P','');
        $address = common::request('address','P','');
        
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!$poi || !$monster_id || !$boss_level){
            $_ENV['error']->getErrors(101);
            exit;
        }
        //更新对战信息
        $row = [
            'poi'=>$poi,
            'boss_type'=>$boss_type,
            'result'=>$result,
            'monster_id'=>$monster_id,
            'boss_level'=>$boss_level,
            'uid'=>$this->uid,
            'city'=>$city,
            'address'=>$address
        ];
        
        $game_mod = $this->load('game');
        $game_mod->addFightResult($row);
        $monster = ['id'=>$monster_id];
        if($result){
            //更新宝宝信息
            $monsterInfo = $game_mod->getMonsterInfo($monster_id);
            $monster = $game_mod->getLevelUpMonster($monsterInfo,$game_mod->getAddExperienceByLevel($boss_level));
            
            //获取宝箱
            $this->result['result']['giftInfo'] = $game_mod->getGift($this->uid,$poi);
        }
        $monster['current_hp'] = $monster_hp;
        $game_mod->updateMonster($monster);
        $this->result['result']['monsterInfo'] = $game_mod->getMonsterInfo($monster_id);
    }
    
    /*
     * 发送礼物
     */
    public function doorderGift(){
        $id = common::request('id','R','');
        $receiver = common::request('receiver','R','');
        $phone = common::request('phone','R','');
        $province = common::request('province','R','');
        $city = common::request('city','R','');
        $district = common::request('district','R','');
        $address = common::request('address','R','');

        /*if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }*/
        if(!common::checkPhone($phone)){
            $_ENV['error']->getErrors(3008);
            exit;
        }
        if(!$id || !$receiver || !$phone || !$address){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $is_delivery = 1;
        $params = compact('id','receiver','phone','province','city','district','address','is_delivery');
        $success = $this->load('game')->recordAdress($params);

        $this->result['result']['is_success'] = $success ? 1 : 0;
    }
    
    /*
     * 照料
     */
    public function docareMonster(){
        $monster_id = common::request('monster_id','P',0);
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!$monster_id){
            $_ENV['error']->getErrors(101);
            exit;
        }
            
        $game_mod = $this->load('game');
        $monsterInfo = $game_mod->getMonsterInfo($monster_id);
        if(!$monsterInfo){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $to_uid = $monsterInfo['uid'];
        
        //客户端
        //扣除正能量
        $client_mod = $this->load('client');
        $userInfo = $client_mod->getUserInfo($this->uid);
        $total_energy = $userInfo['total_energy'];
        if($total_energy < 5){
            $_ENV['error']->getErrors(4002);
            exit;
        }
        $res = $client_mod->userEnergyChange($this->uid,'careMonster',['target_uid'=>$to_uid,'monster_id'=>$monster_id]);
        if($res===false){
            $_ENV['error']->getErrors(4002);
            exit;
        }
        $res = $game_mod->careMonster($this->uid,$monster_id);
        $this->result['result']['monsterInfo'] = $game_mod->getMonsterInfo($monster_id);
        $this->result['result']['success'] = $res ? 1 : 0;
    }
    
    /*
     * 我的宝宝列表
     */
    public function dogetMyMonsters(){
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $rows = $this->db->getAll("select id from r_game_monsters where uid='{$this->uid}'");
        $monsters = [];
        $game_mod = $this->load('game');
        foreach($rows as $row){
            $monsters[] = $game_mod->getMonsterInfo($row['id']);
        }
        $this->result['result']['monsters'] = $monsters;
    }
    
    /*
     * 更新宝宝信息
     */
    public function dopostMonsterInfo(){
        $monster_id = common::request('monster_id','P','');
        $nickname = trim(common::request('nickname','P',''));
        $monster_type = common::request('monster_type','P','');
        
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!$monster_id){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $game_mod = $this->load('game');
        $monsterInfo = $game_mod->getMonsterInfo($monster_id);
        if($this->uid != $monsterInfo['uid']){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        
        $row = [
            'id'=>$monster_id,
        ];
        
        if($nickname){
            $check = $game_mod->checkNickname($nickname);
            $errorMap = array('-1'=>501,'-2'=>502,'-3'=>503);//错误码映射
            if(array_key_exists($check,$errorMap)){
                $_ENV['error']->getErrors($errorMap[$check]);
                exit;
            }
            $row['nickname'] = $nickname;
        }
        
        if($monster_type){
            $row['monster_type'] = $monster_type;
        }
        
        $game_mod->updateMonster($row);
        
        $this->result['result'] = $game_mod->getMonsterInfo($monster_id);
    }

    /*
     * 我的奖品列表
     */
    public function dogetGiftList(){
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $giftList = $this->load('game')->getGiftList($this->uid);
        $this->result['result']['giftList'] = $giftList;
    }
    
    public function dopostGameStatus(){
        $chapter = common::request('chapter','P','');
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!is_numeric($chapter)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $game_mod = $this->load('game');
        $userInfo = $game_mod->getUserInfo($this->uid);
        
        if(empty($userInfo))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        
        $row = [
            'uid' => $this->uid,
            'chapter' => $chapter,
            'chapter_updated_at' => time()
        ];
        $game_mod->updateUser($row);
        
        if($chapter == 4){
            $medal_id=1058;
            $this->load('medal')->addUserMedal($this->uid,$medal_id,date('Ymd'));
            $this->result['result']['medal_id'] = [$medal_id];
        }elseif($chapter == 15){
            //$this->load('medal')->addUserMedal($this->uid,$medal_id,date('Ymd'));
            //$this->result['result']['medal_id'] = [$medal_id];
        }
    }
}